Rules for Python variables:
• A variable name must start with a letter or
the underscore character
• A variable name cannot start with a
number
• A variable name can only contain alpha-numeric characters
and underscores(A-z, 0-9, and_)
• Variable names are case-sensitive
(age, Age and AGE are three different variables)
• A variable name cannot be any of the Python keywords
ExampleLegal variable names:
myvar="John"my_var="John"_my_var="John"myVar="John"MYVAR="John"myvar2="John"ExampleIllegal variable names:
2myvar="John"my-var="John"my var="John"
اضغط لمشاهده الفيديو 👇🏻
The Python print() function is often used to output
variables.
Examplex="Python is awesome"print(x)In the print() function, you output multiple variables, separated by a comma:
Examplex="Python"y="is"z="awesome"print(x, y, z)You can also use the+ operator to output multiple variables:
Examplex="Python"y="is"z="awesome"print(x+y+z)Notice the space character after"Python" and"is", without them the result would be"Pythonisawesome".
For numbers, the+ character works as a mathematical operator:
Examplex=5y=10print(x+y)Python will give you an error:
Example (error)x=5y="John"print(x+y)
شاهد الفيديو التالي هنا👇🏻
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the
indentation in code is for readability only, the indentation in Python is very important. Python uses
indentation to indicate a block of code.
Exampleif 5>2: print("Five is greater than two!")Python
will give you an error if you skip the indentation:
ExampleSyntax
Error:
if 5>2:print("Five is greater than two!")The number of
spaces is up to you as a programmer, the most common use is four, but it has to be at least one.
You
have to use the same number of spaces in the same block of code, otherwise Python will give you an
error:
ExampleSyntax Error:
if 5>2: print("Five
is greater than two!")print("Five is greater than two!")
اضغط لمشاهدة الفيديو هنا 👇🏻
A for loop is used for iterating over a sequence(that is either a list, a tuple, a dictionary, a set,
or a string).
With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
ExamplePrint each fruit in a fruit list:
fruits=["apple","banana","cherry"]for
x in fruits:print(x)
👉🏻شاهد الفيديو على YouTube
With >the break statement we can stop the loop before it has looped through all the items:
1
Example
Exit the loop when x is"banana":
fruits=["apple","banana","cherry"]
for
x in fruits:
print(x)
if x=="banana":
break
2
Example
Exit the loop when x is "banana", but this time the break comes before the
print:
fruits=["apple","banana","cherry"]
for x in
fruits:
if x=="banana":
break
print(x)
With the continue statement we can stop the current iteration of the loop, and continue with the
next:
Example
Do not print banana:
fruits=["apple","banana","cherry"]
for
x in fruits:
if x=="banana":
continue
print(x)
The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:
Example
Print all numbers from 0 to 5, and print a message when the
loop has ended:
for x in range(6):
print(x)
else:
print("Finally
finished!")
With the while loop we can execute a set of statements as long as a condition is
true. Example Print i as long as i is less than 6:
i=1 while i<6:
print(i) i+=1
شاهد الفيديو على YouTube
With the break statement we can stop the loop even if the while condition is
true:
Example
Exit the loop when i is 3:
i=1
while i<6:
print(i)
if i==3:
break
i+=1
With the continue statement we can stop the current iteration, and continue with the
next:
Example
Continue to the next iteration if i is 3:
i=0
while i<6:
i+=1
if i==3:
continue
print(i)
With the else statement we can run a block of code once when the condition no longer is
true:
Example
Print a message once the condition is
false:
i=1
while i<6:
print(i)
i+=1
else:
print("i is no longer less than 6")
Lists are used to store multiple items in a single variable.
شاهد الفيديو لتعلم المزيد👇🏻
There are several methods to add items to a list in Python. The most common methods are append() and insert().
append() - Adds an element to the end of the list.
Syntax:
list.append(element)
Example:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
Output: ["apple", "banana", "cherry", "orange"]
insert() - Adds an element at a specified position.
Syntax:
list.insert(index, element)
Example:
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits)
Output: ["apple", "orange", "banana", "cherry"]
extend() - Adds all elements from an iterable to the end of the list.
Syntax:
list.extend(iterable)
Example:
fruits = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
fruits.extend(tropical)
print(fruits)
Output: ["apple", "banana", "cherry", "mango", "pineapple", "papaya"]
There are several methods to remove items from a list in Python.
remove() - Removes the first occurrence of a specified value.
Syntax:
list.remove(value)
Example:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
Output: ["apple", "cherry"]
pop() - Removes and returns the element at the specified index.
Syntax:
list.pop(index)
Example:
fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits)
Output: ["apple", "cherry"]
clear() - Removes all elements from the list.
Syntax:
list.clear()
Example:
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)
Output: []
Python lists have a built-in sort() method that modifies the list in-place.
sort() - Sorts the list in ascending order by default.
Syntax:
list.sort()
Example:
fruits = ["banana", "apple", "cherry"]
fruits.sort()
print(fruits)
Output: ["apple", "banana", "cherry"]
Use the reverse=True parameter to sort in descending order.
Syntax:
list.sort(reverse=True)
Example:
numbers = [100, 50, 65, 82, 23]
numbers.sort(reverse=True)
print(numbers)
Output: [100, 82, 65, 50, 23]
Complete List of Python List Methods:
| Method | Description |
append() | Adds an element to the end |
clear() | Removes all elements |
copy() | Returns a copy of the list |
count() | Returns count of an element |
extend() | Adds elements from an iterable |
index() | Returns index of first occurrence |
insert() | Adds element at specified position |
pop() | Removes and returns element at index |
remove() | Removes first occurrence of value |
reverse() | Reverses the list order |
sort() | Sorts the list |